package com.library.uiframework.mvp;

import java.util.List;

/**
 * @param <VIEW>  view of mvp
 * @author xinpeng.xiao
 * @version 2.0.0
 *          Created on 2016/1/21.
 *          modified on 2017/03/17
 */
public abstract class PagingDataLoadPresenter<VIEW extends PagingDataLoadView> extends BasePresenter<VIEW> {
    private static final String TAG = "PagingDataLoadPresenter";

    private static final int STATE_IDLE = 0;
    private static final int STATE_LOADING = 1;
    private static final int STATE_FAILED = 2;
    private static final int STATE_ALL_PAGE_LOADED = 3;

    /**
     * 默认的起始页
     */
    protected static final int DEFAULT_PAGE_START = 1;

    private int mLoadingPage = DEFAULT_PAGE_START;
    private int mState = STATE_IDLE;

    /**
     * @param pageDataLoadingView pageDataLoadingView
     */
    protected PagingDataLoadPresenter(VIEW pageDataLoadingView) {
        super(pageDataLoadingView);
    }


    /**
     * 重新开始加载数据
     */
    public void reload() {
        reload(true);
    }

    public void reload(boolean force) {
        if (isLoading()) {
//            MLog.d(TAG, "PageLoadingPresenter.loadFirstPage ignore, since already loading");
            return;
        }
        mLoadingPage = DEFAULT_PAGE_START;
        loadNextPageInner(force, mLoadingPage);
    }

    /**
     *
     */
    public void loadNextPage() {
        if (isLoading()) {
//            MLog.d(TAG, "PageLoadingPresenter.loadNextPage ignore, since already loading");
            return;
        }

        /** 注意,这里不会更改 {@link #mLoadingPage} 的值*/

        loadNextPageInner(false, mLoadingPage);
    }

    private void loadNextPageInner(boolean force, int loadPage) {
        mState = STATE_LOADING;

        if (loadPage == DEFAULT_PAGE_START) {
            if (!hasExtraData() && mBindView.isDataEmpty()) {
                mBindView.showLoading();
            }
        } else {
            mBindView.showNextPageLoading();
        }

        load(loadPage, force);
    }

    protected boolean isLoading() {
        return mState == STATE_LOADING;
    }

    /**
     * @param page  加载第几页
     * @param force 是否强制加载  强制加载即列表中有数据的时候, 否则是无数据的时候, 此参数这两年基本没用过, 为兼容保留
     */
    protected abstract void load(int page, boolean force);

    /**
     * @param dataList  当前加载成功的数据
     * @param totalPage 总页数
     * @param page      当前加载成功的页
     */
    protected void handleLoadSuccess(List dataList, int totalPage, int page) {
        mLoadingPage = page;

        // 以下所有情况, 都是接口请求成功的各种状态
        // 对第一页/非第一页做区分, 是因为展示/处理不一样

        if (mLoadingPage <= DEFAULT_PAGE_START) {
            // 加载第一页成功了

            mBindView.flushData(dataList);
            if (dataList == null || dataList.isEmpty()) {
                // 第一页没有数据
                if (hasExtraData()) {
                    mBindView.showAllPageLoaded();
                    mBindView.showSuccess();
                } else {
                    mBindView.showNoData();
                }
                mState = STATE_ALL_PAGE_LOADED;
            } else {
                // 刷数据
                mBindView.showSuccess();
                ++mLoadingPage;

                if (mLoadingPage > totalPage) {
                    // 所有的数据只有一页
                    mState = STATE_ALL_PAGE_LOADED;
                    mBindView.showAllPageLoaded();
                } else {
                    // 还有下一页
                    mState = STATE_IDLE;
                    mBindView.showNextPageSuccess();
                }
            }
        } else {
            // 加载分页成功了

            // 刷数据
            mBindView.appendData(dataList);

            if (totalPage > mLoadingPage) {
                // 还有下一页
                mBindView.showNextPageSuccess();
                ++mLoadingPage;
                mState = STATE_IDLE;
            } else {
                // 没有下一页, 所有数据都完成了
                mState = STATE_ALL_PAGE_LOADED;
                mBindView.showAllPageLoaded();
            }
        }
    }

    /**
     * @param e exception
     */
    protected void handleLoadFailure(Throwable e) {
        if (mLoadingPage <= DEFAULT_PAGE_START) {
            // 加载第一页失败了
            mBindView.flushData(null); //原来有数据的话要清除, 避免浪费内存
            mBindView.showFailure(e != null ? e.getMessage() : null);
        } else {
            // 加载分页数据失败了
            mBindView.showNextPageFailure();
        }

        mState = STATE_FAILED;
    }

    /**
     * @return 除了列表中的数据,其他是否有算作不需要显示空白页的数据条数
     */
    protected boolean hasExtraData() {
        return false;
    }

    /**
     * @return 当前请求的面数
     */
    public int getLoadingPage() {
        return mLoadingPage;
    }

    public void setLoadingPage(int mLoadingPage) {
        this.mLoadingPage = mLoadingPage;
    }

    protected VIEW getView() {
        return mBindView;
    }

    boolean canAutoLoadNextPage() {
        return mState == STATE_IDLE || mState == STATE_FAILED;
    }

    /**
     * 重置加载状态
     */
    public void reset() {
        mState = STATE_IDLE;
        mLoadingPage = DEFAULT_PAGE_START;
    }
}
